package com.personnel.rbac.action;

import org.apache.log4j.Logger;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.personnel.action.BaseAction;
import com.personnel.constant.NodeType;
import com.personnel.model.BaseEntity;
import com.personnel.rbac.model.Department;
import com.personnel.rbac.model.view.VDeptUser;
import com.personnel.utils.StringUtil;
/**
 * 部门Action
 * @author xzl
 *
 */
@Component("deptAction")
@Scope("prototype")
public class DeptAction extends BaseAction {
	private static Logger logger=Logger.getLogger(DeptAction.class);
	/**
	 * 
	 */
	private static final long serialVersionUID = -7000507770386894691L;
	private Department department=new Department();
	@Override
	public Object getModel() {
		// TODO Auto-generated method stub
		String parentId=request.getParameter("parentId");
		if(StringUtil.isEmpty(parentId)){
			parentId=NodeType.ROOT;
		}
		Department parent=new Department();
		parent.setDeptId(parentId);
		department.setParent(parent);
		return department;
	}
	public void doSave(){
		Object entity=getModel();
		try{
			if(entity instanceof BaseEntity){
				buildModelCreateInfo((BaseEntity)entity);
			}else{
				logger.error("实体信息获取错误");
				toWrite(jsonBuilder.returnFailureJson("'传入的实体信息错误'"));
				return;
			}
			//构建创建信息
			
			//保存实体
			entity=pcServiceTemplate.save(entity);
			Department dept=(Department) entity;
			Department parent=(Department) pcServiceTemplate.findById(Department.class, dept.getParent().getDeptId());
			if(!parent.getDeptId().equals(NodeType.ROOT)){
				parent.setNodeType(NodeType.GENERAL);
				pcServiceTemplate.update(parent);
				VDeptUser vdu = (VDeptUser) pcServiceTemplate.findById(VDeptUser.class, parent.getDeptId());
				updateParentView(vdu);
			}
			toWrite(jsonBuilder.returnSuccessJson(jsonBuilder.toJson(entity)));
		}catch(Exception e){
			logger.error("保存方法出错，错误信息"+e.getMessage());
			toWrite(jsonBuilder.returnFailureJson("'保存方法出错，错误信息"+e.getMessage()+"'"));
		}
	}
	/**
	 * 重写部门的更新
	 */
	public void doUpdate(){
		Object entity=getModel();
		try{
			if(entity instanceof BaseEntity){
				buildModelModifyInfo((BaseEntity)entity);
			}else{
				logger.error("实体信息获取错误");
				toWrite(jsonBuilder.returnFailureJson("'传入的实体信息错误'"));
				return;
			}
			entity=pcServiceTemplate.formUpdate(entity);//返回的信息不完整？
			Department dept=(Department) pcServiceTemplate.findById(Department.class, ((Department)entity).getDeptId());
			VDeptUser vdu = (VDeptUser) pcServiceTemplate.findById(VDeptUser.class, dept.getDeptId());
			//同时保存在“视图表”,并更新“视图表”中的父结点
			updateView(dept,vdu);
			toWrite(jsonBuilder.returnSuccessJson(jsonBuilder.toJson(entity)));
		}catch(Exception e){
			logger.error("更新方法出错，错误信息"+e.getMessage());
			toWrite(jsonBuilder.returnFailureJson("'更新方法出错，错误信息"+e.getMessage()+"'"));
		}
	}
	/**
	 * 
	 * @param dept
	 * @param vdu
	 */
	public void updateView(Department dept,VDeptUser vdu){
		if(vdu==null){
			StringBuffer sql = new StringBuffer("insert into vdeptuser ");
			sql.append("(id,text,code,nodeType,NodeInfo,nodeInfoType,parent,orderIndex,disabled,icon) ");
			sql.append("values('"+dept.getDeptId()+"',");
			sql.append("'"+dept.getDeptName()+"',");
			sql.append("'"+dept.getDeptCode()+"',");
			sql.append("'"+dept.getNodeType()+"',");
			sql.append("'Department',");
			sql.append("'"+dept.getNodeInfoType()+"',");
			sql.append("'"+dept.getParent().getDeptId()+"',");
			sql.append("'"+dept.getOrderIndex()+"',");
			sql.append("'true',");
			sql.append("'core/css/image/tree/tree_user.png'");
			sql.append(")");
			pcServiceTemplate.executeSql(sql.toString());
		}else{
			StringBuffer sql = new StringBuffer("update vdeptuser v set ");
			sql.append("v.text ='"+dept.getDeptName()+"',");
			sql.append("v.code ='"+dept.getDeptCode()+"',");
			sql.append("v.nodeType ='"+dept.getNodeType()+"',");
			sql.append("v.NodeInfo ='Department',");
			sql.append("v.nodeInfoType ='"+dept.getNodeInfoType()+"',");
			sql.append("v.parent ='"+dept.getParent().getDeptId()+"',");
			sql.append("v.orderIndex ='"+dept.getOrderIndex()+"',");
			sql.append("v.disabled ='true',");
			sql.append("v.icon ='core/css/image/tree/tree_user.png' where v.id='"+vdu.getId()+"'");
			pcServiceTemplate.executeSql(sql.toString());
		}
	}
	/**
	 * 更新父结点的"视图" 
	 * @param vdu
	 */
	public void updateParentView(VDeptUser vdu){
		StringBuffer sql = new StringBuffer("update vdeptuser v set v.nodeType="+NodeType.GENERAL+"where v.id='"+vdu.getId()+"'");
		pcServiceTemplate.executeSql(sql.toString());
	}
}
